<template>
  <div class="information-warp">
    <el-card class="box-card">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
        style="width: 400px; margin: 0 auto;"
      >
        <el-form-item label="ID" prop="user_id" ref="user_id">
          <el-input v-model.number="ruleForm.user_id" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="user_name">
          <el-input v-model="ruleForm.user_name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="user_sex">
          <el-radio-group v-model="ruleForm.user_sex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="user_age">
          <el-input v-model="ruleForm.user_age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="user_phone">
          <el-input v-model="ruleForm.user_phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="系别" prop="dept_id">
          <el-select v-model="ruleForm.dept_id" placeholder="请选择" disabled>
            <el-option
              v-for="item in deptList"
              :key="item.dept_id"
              :label="item.dept_name"
              :value="item.dept_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="ruleForm.role" placeholder="请选择" disabled>
            <el-option label="学生" value="student"></el-option>
            <el-option label="教师" value="teacher"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getUserInfo, editUser } from "@/api/user";
import { deptList } from "@/api/dept";
export default {
  name: 'Information',

  data() {
    const validateMobile = (rule, value, callback) => {
      const reg = /^1[3456789]\d{9}$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('请输入正确的手机号码'))
      }
    }
    return {
      ruleForm: {
        user_id: '',
        user_name: '',
        user_sex: 0,
        user_age: '',
        user_phone: '',
        dept_id: null,
        role: 'student'
      },
      rules: {
        user_name: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }
        ],
        user_phone: [
          {
            validator: validateMobile,
            trigger: 'blur'
          }
        ],
      },
      deptList: [],
    }
  },

  methods: {
    // 1. 获取个人信息
    userDetail() {
      getUserInfo()
        .then(res => {
          if (res.result) {
            res.data.role = res.data.role[0]
            res.data.user_sex = Number(res.data.user_sex);
            this.ruleForm = { ...res.data }
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },

    // 2. 获取系别列表
    getDeptList() {
      deptList().then(res => {
        if (res.result) {
          this.deptList = res.data
        }
      })
    },

    // 3. 修改个人信息
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          editUser(this.ruleForm).then(res => {
            if (res.result) {
              this.$message.success(res.message)
              this.userDetail();
            } else {
              this.$message.error(res.message)
            }
          })
            .catch(err => {
              this.$message.error(err.message)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  },

  mounted() {
    this.userDetail();
    this.getDeptList();
  }
}
</script>

<style lang="stylus" scoped>
.footer 
  text-align center
  margin 40px auto
.el-select 
  width 100%
</style>
